

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="叶尘的技术升级小木屋">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>7、认识面向对象 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="7、认识面向对象">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-09-13 22:04" pubdate>
        星期一, 九月 13日 2021, 10:04 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      23
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">7、认识面向对象</h1>
            
            <div class="markdown-body">
              <h3 id="7-1-面向对象和面向过程"><a href="#7-1-面向对象和面向过程" class="headerlink" title="7.1 面向对象和面向过程"></a>7.1 面向对象和面向过程</h3><ul>
<li><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=373">视频详解</a></p>
</li>
<li><p>使用面向对象编程思想开发系统，在现代开发中会将面向对象贯穿整个过程，一般包括：</p>
<ul>
<li>OOA：面向对象分析（Object-Oriented Analysis） </li>
<li>OOD：面向对象设计（Object-Oriented Design）</li>
<li>OOP：面向对象编程（Object-Oriented Programming）</li>
</ul>
</li>
<li><p><strong>面向对象三大特征</strong></p>
<ul>
<li>封装（Encapsulation） </li>
<li>继承（Inheritance） </li>
<li>多态（Polymorphism）</li>
</ul>
</li>
</ul>
<h3 id="7-2-类和对象的概念-很枯燥但很重要"><a href="#7-2-类和对象的概念-很枯燥但很重要" class="headerlink" title="7.2 类和对象的概念(很枯燥但很重要)"></a>7.2 类和对象的概念(很枯燥但很重要)</h3><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=379">视频讲解</a></p>
<h4 id="7-2-1-什么是类"><a href="#7-2-1-什么是类" class="headerlink" title="7.2.1 什么是类"></a>7.2.1 什么是类</h4><ul>
<li>类实际上在现实世界当中是==不存在的，是一个抽象的概念==。是一个模板。是我们人类大脑进行“思考、总结、抽象”的一个结果。</li>
<li>类本质上是现实世界当中某些事物具有共同特征，将这些共同特征提取出来形成的概念就是一个“类”，==“类”就是一个模板==。</li>
</ul>
<h4 id="7-2-2-什么是对象"><a href="#7-2-2-什么是对象" class="headerlink" title="7.2.2 什么是对象"></a>7.2.2 什么是对象</h4><ul>
<li>==对象是实际存在的个体==</li>
<li>在java语言中，要想得到“对象”，必须先定义“类”，<strong>“对象”是通过“类”这个模板创造出来的</strong>。</li>
</ul>
<h4 id="7-2-3-术语"><a href="#7-2-3-术语" class="headerlink" title="7.2.3 术语"></a>7.2.3 术语</h4><ul>
<li>类：不存在的，人类大脑思考中总结的一个模板</li>
<li>对象：实际存在的个体，通过 “类” 创造出来</li>
<li>实例：对象的另一个名字</li>
<li>实例化：通过类这个模板将对象创建出来的过程叫做实例化</li>
<li>抽象：多个对象具有的共同特征，进行思考总结抽取共同特征的过程</li>
<li>==类 —&gt; 【实例化】 —&gt; 对象（实例）==</li>
<li>==对象 —&gt; 【抽象】 —&gt; 类==</li>
</ul>
<h4 id="7-2-4-类的定义"><a href="#7-2-4-类的定义" class="headerlink" title="7.2.4 类的定义"></a>7.2.4 类的定义</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java">[修饰符] <span class="hljs-class"><span class="hljs-keyword">class</span> 类名 </span>&#123;<br> 	<span class="hljs-comment">// 类体 = 属性 + 方法</span><br>    <span class="hljs-comment">// 属性在代码上以“变量”的形式存在</span><br>    <span class="hljs-comment">// 方法描述动作或行为</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>定义一个学生类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> </span>&#123;<br>    <span class="hljs-comment">// 以下四个变量即为 属性</span><br>    <span class="hljs-keyword">int</span> sno;<span class="hljs-comment">// 学号</span><br>    String name;<span class="hljs-comment">// 姓名</span><br>    <span class="hljs-keyword">boolean</span> gender;<span class="hljs-comment">// 性别（true表示男，false表示女）</span><br>    String address;<span class="hljs-comment">// 住址</span><br>    <br>    <span class="hljs-comment">// 以下是一个方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">print</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(name + <span class="hljs-string">&quot;的学号是：&quot;</span> + sno + <span class="hljs-string">&quot;，性别是：&quot;</span> + (gender ? <span class="hljs-string">&quot;男&quot;</span> : <span class="hljs-string">&quot;女&quot;</span>) + <span class="hljs-string">&quot;，住在：&quot;</span> + address);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="7-2-5-对象创建"><a href="#7-2-5-对象创建" class="headerlink" title="7.2.5 对象创建"></a>7.2.5 对象创建</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java">类名 变量名 = <span class="hljs-keyword">new</span> 类名();<span class="hljs-comment">// 这样就完成了对象的创建</span><br><span class="hljs-comment">// 如</span><br>Student student = <span class="hljs-keyword">new</span> Student();<br></code></pre></td></tr></table></figure>

<ul>
<li>什么是实例变量：对象又被称为实例变量，实例变量在实际上就是：对象级别的变量</li>
<li>不能通过类名来直接访问实例变量，需要new对象后，通过 <strong>“对象名.实例变量”</strong> 才能访问实例变量</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 如People类中的name变量就是实例变量</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>&#123;<br>    String name;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="7-2-6-创建对象对应的-JVM-内存结构"><a href="#7-2-6-创建对象对应的-JVM-内存结构" class="headerlink" title="7.2.6 创建对象对应的 JVM 内存结构"></a>7.2.6 创建对象对应的 JVM 内存结构</h4><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=385">视频讲解</a></p>
<p>以下是一个学生类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span></span>&#123;<br>	<span class="hljs-comment">// 属性（描述状态），在java程序中以“成员变量”的形式存在。</span><br>	<span class="hljs-comment">// 学号</span><br>	<span class="hljs-comment">// 一个对象一份。</span><br>	<span class="hljs-keyword">int</span> no; <span class="hljs-comment">// 这种成员变量又被称为“实例变量”。</span><br>	<span class="hljs-comment">// 姓名</span><br>	String name;<br>	<span class="hljs-comment">// 年龄</span><br>	<span class="hljs-keyword">int</span> age;<br>	<span class="hljs-comment">// 性别</span><br>	<span class="hljs-keyword">boolean</span> sex;<br>	<span class="hljs-comment">// 住址</span><br>	String addr;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>以下main方法中使用学生类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StudentTest</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        Student s1 = <span class="hljs-keyword">new</span> Student();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/20210206110351783.png" srcset="/img/loading.gif" lazyload alt="image-20210206110147553"></p>
<h4 id="7-2-7-对象和引用的区别"><a href="#7-2-7-对象和引用的区别" class="headerlink" title="7.2.7 对象和引用的区别"></a>7.2.7 对象和引用的区别</h4><ul>
<li>对象是 new 出来的，在堆内存中存储</li>
<li>是一个变量，这个变量中储存了一个内存地址，指向了对象在堆内存中的地址</li>
</ul>
<h4 id="7-2-8-系统默认值"><a href="#7-2-8-系统默认值" class="headerlink" title="7.2.8 系统默认值"></a>7.2.8 系统默认值</h4><p>对于成员变量来说，没有手动赋值时，系统默认赋值</p>
<table>
<thead>
<tr>
<th>数据类型</th>
<th>缺省默认值</th>
</tr>
</thead>
<tbody><tr>
<td>byte</td>
<td>0</td>
</tr>
<tr>
<td>short</td>
<td>0</td>
</tr>
<tr>
<td>int</td>
<td>0</td>
</tr>
<tr>
<td>long</td>
<td>0L</td>
</tr>
<tr>
<td>float</td>
<td>0.0f</td>
</tr>
<tr>
<td>double</td>
<td>0.0</td>
</tr>
<tr>
<td>boolean</td>
<td>false</td>
</tr>
<tr>
<td>cher</td>
<td>‘\u0000’</td>
</tr>
<tr>
<td>引用数据类型</td>
<td>null</td>
</tr>
</tbody></table>
<h4 id="7-2-9-画以下程序的内存图"><a href="#7-2-9-画以下程序的内存图" class="headerlink" title="7.2.9 画以下程序的内存图"></a>7.2.9 画以下程序的内存图</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">T</span></span>&#123;<br>	A o1; <br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>&#123;<br>		D d = <span class="hljs-keyword">new</span> D();<br>		C c = <span class="hljs-keyword">new</span> C();<br>		B b = <span class="hljs-keyword">new</span> B();<br>		A a = <span class="hljs-keyword">new</span> A();<br>		T t = <span class="hljs-keyword">new</span> T();<br><br>		c.o4 = d;<br>		b.o3 = c;<br>		a.o2 = b;<br>		t.o1 = a;<br><br>		System.out.println(t.o1.o2.o3.o4.i);<br>	&#125;<br>&#125;<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span></span>&#123;<br>	B o2;<br>&#125;<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span></span>&#123;<br>	C o3;<br>&#125;<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">C</span></span>&#123;<br>	D o4;<br>&#125;<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">D</span></span>&#123;<br>	<span class="hljs-keyword">int</span> i;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/20210206114813124.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYwMTA0MA==,size_16,color_FFFFFF,t_70#pic_center" srcset="/img/loading.gif" lazyload alt="内存图"></p>
<h4 id="7-2-10-空指针异常（NullPointerException）"><a href="#7-2-10-空指针异常（NullPointerException）" class="headerlink" title="7.2.10 空指针异常（NullPointerException）"></a>7.2.10 空指针异常（NullPointerException）</h4><p><strong>==导致空指针异常最本质的原因是：空引用访问 “实例相关的数据”（包括实例变量 + 实例方法），会出现空指针异常==</strong></p>
<p>观察以下程序：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/*</span><br><span class="hljs-comment">	空指针异常。（NullPointerException）</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">	关于垃圾回收器：GC</span><br><span class="hljs-comment">		在java语言中，垃圾回收器主要针对的是堆内存。</span><br><span class="hljs-comment">		当一个java对象没有任何引用指向该对象的时候，</span><br><span class="hljs-comment">		GC会考虑将该垃圾数据释放回收掉。</span><br><span class="hljs-comment">	</span><br><span class="hljs-comment">	出现空指针异常的前提条件是？</span><br><span class="hljs-comment">		&quot;空引用&quot;访问实例【对象相关】相关的数据时，都会出现空指针异常。</span><br><span class="hljs-comment">*/</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NullPointerTest</span></span>&#123;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>&#123;<br>		<span class="hljs-comment">// 创建客户对象</span><br>		Customer c = <span class="hljs-keyword">new</span> Customer();<br>		<span class="hljs-comment">// 访问这个客户的id</span><br>		System.out.println(c.id); <span class="hljs-comment">// 0</span><br><br>		<span class="hljs-comment">// 重新给id赋值</span><br>		c.id = <span class="hljs-number">9521</span>; <span class="hljs-comment">// 终身代号</span><br>		System.out.println(<span class="hljs-string">&quot;客户的id是=&quot;</span> + c.id);<br><br>		c = <span class="hljs-keyword">null</span>;<br>		<span class="hljs-comment">// NullPointerException</span><br>		<span class="hljs-comment">// 编译器没问题，因为编译器只检查语法，编译器发现c是Customer类型，</span><br>		<span class="hljs-comment">// Customer类型中有id属性，所以可以：c.id。语法过了。</span><br>		<span class="hljs-comment">// 但是运行的时候需要对象的存在，但是对象没了，尴尬了，就只能出现一个异常。</span><br>		System.out.println(c.id);<br>	&#125;<br>&#125;<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Customer</span></span>&#123;<br>	<span class="hljs-comment">// 客户id</span><br>	<span class="hljs-keyword">int</span> id; <span class="hljs-comment">// 成员变量中的实例变量，应该先创建对象，然后通过“引用.”的方式访问。</span><br>&#125;<br></code></pre></td></tr></table></figure>



<p><img src="https://img-blog.csdnimg.cn/20210206122223128.png" srcset="/img/loading.gif" lazyload alt="image-20210206122203931"></p>
<h4 id="7-2-11-方法调用时的参数传递"><a href="#7-2-11-方法调用时的参数传递" class="headerlink" title="7.2.11 方法调用时的参数传递"></a>7.2.11 方法调用时的参数传递</h4><ul>
<li>参数传递的时候，和类型无关，不管是基本数据类型还是引用数据类型统一都是将变量中保存的那个“值”复制一份，传递下去，这个值可能是一个数字、字符串、可能是一个内存地址。</li>
</ul>
<h3 id="7-3-构造方法"><a href="#7-3-构造方法" class="headerlink" title="7.3 构造方法"></a>7.3 构造方法</h3><blockquote>
<p> <strong>构造方法使用来创建对象的，并且给对象当中的实例变量赋值。</strong></p>
<p> <strong>当一个类中没有提供任何构造方法，系统默认提供一个无参数的构造方法。这个==无参数的构造方法叫做缺省构造器==。</strong></p>
<p> <strong>当一个类中手动提供了构造方法，那么系统将不在提供默认的无参构造方法。</strong></p>
</blockquote>
<ul>
<li>构造方法的语法结构<ul>
<li>构造方法名和类名必须一致</li>
<li>构造方法不需要指定返回值类型，也不能写void</li>
<li>通常在构造方法体当中给属性赋值，完成属性的初始化</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java">[修饰符列表] 构造方法名(形式参数列表)&#123;<br>    构造方法体;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>调用构造方法怎么调用呢<ul>
<li>使用new运算符来调用构造方法</li>
<li>语法格式：</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">new</span> 构造方法名(实际参数列表);<br></code></pre></td></tr></table></figure>

<ul>
<li><p>构造方法支持方法重载：</p>
<ul>
<li>构造方法是支持方法重载的</li>
<li>在一个类当中构造方法可以有多个</li>
<li>并且所有的构造方法名字都是一样的</li>
</ul>
</li>
<li><p>对于实例变量来说，==只要你在构造方法中没有手动给它赋值，统一都会默认赋值==。默认赋系统值。如：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vip</span></span>&#123;<br>	<span class="hljs-comment">// 会员号</span><br>	<span class="hljs-keyword">long</span> no;<br>	<span class="hljs-comment">// 会员姓名</span><br>	String name;<br>	<span class="hljs-comment">// 生日</span><br>	String birth;<br>	<span class="hljs-comment">// 性别</span><br>	<span class="hljs-keyword">boolean</span> sex;<br>	<span class="hljs-comment">//无参数构造方法</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Vip</span><span class="hljs-params">()</span></span>&#123;<br>		<span class="hljs-comment">// 实际上这里还有两行代码（没有手动赋值，系统都会默认赋值。）</span><br>        <span class="hljs-comment">// no = 0L;</span><br>        <span class="hljs-comment">// name = null;</span><br>        <span class="hljs-comment">// birth = null;</span><br>        <span class="hljs-comment">// sex = false;</span><br>	&#125;<br><br>	<span class="hljs-comment">//有参数构造方法</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Vip</span><span class="hljs-params">(<span class="hljs-keyword">long</span> huiYuanHao, String xingMing)</span></span>&#123;<br>		<span class="hljs-comment">// 给实例变量赋值【初始化实例变量，初始化属性】</span><br>		no = huiYuanHao;<br>		name = xingMing;<br>		<span class="hljs-comment">// 实际上这里还有两行代码（没有手动赋值，系统都会默认赋值。）</span><br>		<span class="hljs-comment">//birth = null;</span><br>		<span class="hljs-comment">//sex = false;</span><br>	&#125;<br><br>	<span class="hljs-comment">//有参数构造方法</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Vip</span><span class="hljs-params">(<span class="hljs-keyword">long</span> huiYuanHao,String xingMing, String shengRi)</span></span>&#123;<br>		no = huiYuanHao;<br>		name = xingMing;<br>		birth = shengRi;<br>		<span class="hljs-comment">// 实际上这里有一行默认的代码</span><br>		<span class="hljs-comment">//sex = false;</span><br>	&#125;<br><br>	<span class="hljs-comment">//有参数的构造方法</span><br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Vip</span><span class="hljs-params">(<span class="hljs-keyword">long</span> huiYuanHao,String xingMing,String shengRi,<span class="hljs-keyword">boolean</span> xingBie)</span></span>&#123;<br>		no = huiYuanHao;<br>		name = xingMing;<br>		birth = shengRi;<br>		sex = xingBie;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E5%B0%81%E8%A3%85/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">8、封装</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E6%96%B9%E6%B3%95%E4%B8%8E%E9%80%92%E5%BD%92/">
                        <span class="hidden-mobile">6、方法与递归</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
